/**
 * WebOrderForm.java
 * com.sftj.web.order.form
 * Copyright (c) 2017, 北京科技有限公司版权所有.
*/

package com.sftz.modules.minapp.order.form;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

import lombok.Data;

import org.nutz.dao.Cnd;
import org.nutz.dao.SqlManager;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;

import com.uxuexi.core.common.util.DateTimeUtil;
import com.uxuexi.core.common.util.DateUtil;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.web.form.SQLParamForm;

@Data
/*@EqualsAndHashCode(callSuper = true)*/
public class WebOrderForm implements SQLParamForm, Serializable {
	private static final long serialVersionUID = 1L;
	/**id*/
	private Integer id;

	/**订单编号*/
	private String orderNum;

	/**订单编号*/
	private String orderId;

	/**用户Id*/
	private int userId;

	/**账号*/
	private String username;

	/**金额*/
	private Double amountOfMoney;

	/**订单状态*/
	private Integer orderState;

	/**订单创建时间*/
	private Date orderCreateTime;

	/**查询开始时间*/
	private Date startDate;

	/**查询结束时间*/
	private Date endDate;

	/**订单完成时间*/
	private Date orderCompleteTime = DateTimeUtil.nowDate();

	/**学院id*/
	private Integer collegeId;

	/**学院名称*/
	private String collegeName;

	/**商品id*/
	private Integer productId;

	/**商品名称*/
	private String productName;

	/**订单类型*/
	private Integer orderType;

	/**注册状态*/
	private Integer registerState;

	/**页面查询结束时间*/
	private Timestamp queryTimeEnd;

	@Override
	public Sql sql(SqlManager sqlManager) {
		Sql sql = Sqls.create(sqlManager.get("web_order_list"));
		sql.setCondition(cnd());
		return sql;
	}

	private Cnd cnd() {
		Cnd cnd = Cnd.NEW();

		/**结束时间如果输入日期yyyy-MM-dd，则结束时间需要取当日的最后一秒*/
		Timestamp end = null;
		if (!Util.isEmpty(queryTimeEnd)) {
			end = new Timestamp(queryTimeEnd.getTime() + (DateUtil.MILLIS_OF_DAY - 1));
		}
		//起始时间
		if (!Util.isEmpty(startDate) && Util.isEmpty(end))
			cnd.and("orderCreateTime", ">=", startDate);

		if (Util.isEmpty(startDate) && !Util.isEmpty(end))
			cnd.and("orderCreateTime", "<=", end);

		if (!Util.isEmpty(startDate) && !Util.isEmpty(end)) {
			cnd.and("orderCreateTime", ">=", startDate);
			cnd.and("orderCreateTime", "<=", end);
		}
		if (!Util.isEmpty(orderState)) {
			cnd.and("orderState", "=", orderState);
		}
		if (!Util.isEmpty(collegeName)) {
			cnd.and("collegeName", "like", "%" + collegeName + "%");
		}
		cnd.and("ca.userId", "=", 3);
		return cnd;
	}
}